package com.iflytek.uoamp.agent.monitor.job;

import com.iflytek.uoamp.agent.common.util.DirectoryControl;
import com.iflytek.uoamp.agent.common.util.ReadFile;
import com.iflytek.uoamp.agent.common.util.WFile;
import com.iflytek.uoamp.agent.core.sender.monitorsender.requestObj.ItemTypes;
import com.iflytek.uoamp.agent.core.sender.monitorsender.requestObj.MonitorItemConfig;
import com.iflytek.uoamp.agent.monitor.mcagent.MCAgentMain;

import org.apache.log4j.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gaowei
 * 读写本地缓存;
 */
public class Cache {

	private static final String DESDATAFILE = "DataFile";
	private static final String DATAFILEPATH = DESDATAFILE + File.separator;
	
	public static void cleanCache(){
		DirectoryControl.delAllFile(DESDATAFILE);
		Logger.getRootLogger().info("del All Config File");
	}
	
	public static String getConfigDiv(){
		String confFilePath = DATAFILEPATH + ItemTypes.caseToString(ItemTypes.CHECKLOCAL) + File.separator;
		return confFilePath;
	}
	
	public static String getConfigFile(String file){
		file = file.replaceAll("/", "_");
		String confFilePath = DATAFILEPATH + ItemTypes.caseToString(ItemTypes.CHECKLOCAL) + File.separator + file + ".conf";
		confFilePath = confFilePath.replaceAll(":", "_");
		confFilePath = confFilePath.replaceAll("\\(", "_");
		confFilePath = confFilePath.replaceAll("\\)", "_");
		return confFilePath;
	}
	
	/**
	 * 单独更新本地文件
	 * @param config
	 * @return
	 */
	public static boolean cacheItemConfFile(MonitorItemConfig config){
		DirectoryControl.makdir(getConfigDiv());
		boolean isOK = WFile.wfByByte(getConfigFile(config.getServerDesc()), config.toByte().array());
		Logger.getRootLogger().info(config.getServerDesc()+"  write file and "+isOK);
		return isOK;
	}
	
	/**
	 * 读取本地缓存文件;
	 * @param itemType
	 * @param serviceID
	 * @return
	 */
	public static MonitorItemConfig readCache(String itemType, String serviceID) {
//		byte[] fileByteContent = ReadFile.readAll4Byte(getConfigFile(serviceID));
        String fileByteContent = ReadFile.readAll(getConfigFile(serviceID) ,"UTF-8");
		if(fileByteContent==null){
			Logger.getRootLogger().error("i can't read "+itemType+","+serviceID+"  file!!");
			return null;
		}
		return MonitorItemConfig.getFromString(fileByteContent);
	}
	
	/**
	 * 删除本地文件;
	 * @param file
	 */
	public static void delFile(String file){
		DirectoryControl.delFile(getConfigFile(file));
	}
	
	/**
	 * 更新本地缓存文件;会删除无用的文件
	 * 如果文件被删除后可以再写入；则视为更新；
	 * @param list
	 * @return
	 */
	public static List<MonitorItemConfig> updateCache(List<MonitorItemConfig> list){
		List<MonitorItemConfig> result = new ArrayList<MonitorItemConfig>();
		for (MonitorItemConfig config : list) {
			delFile(config.getServerDesc());
			MCAgentMain.getCheckNames().remove(config.getServerDesc());
			Logger.getRootLogger().info(config.getServerDesc()+"   delete file sucess");
			
			if (config.getServerUrl()==null) {
				continue;
			} else {
				cacheItemConfFile(config);
			}
			result.add(config);
			MCAgentMain.getCheckNames().add(config.getServerDesc());
			Logger.getRootLogger().info(config.getServerDesc()+"   write file sucess");
		}
		return result;
	}
	
}	
